package cn.com.jrr.core.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.jrr.core.common.ToolUtils;
import cn.com.jrr.core.anotation.SystemControllerLog;
import cn.com.jrr.core.model.DataTableRequest;
import cn.com.jrr.core.model.DataTableResponse;
import cn.com.jrr.core.model.ResponseMessage;
import cn.com.jrr.core.model.School;
import cn.com.jrr.core.model.Student;
import cn.com.jrr.core.service.SchoolService;
import cn.com.jrr.core.service.StudentService;

@Controller
@RequestMapping("/student")
public class StudentAction {
    @Autowired
    private StudentService studentService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private Student student;
    @Autowired
    private ResponseMessage responseMessage;
    private DataTableResponse<Student> dt;
    /**
     * 锟斤拷始锟斤拷
     *
     */

    @RequestMapping("/stu")
    public String info(){
        return "/student/student";
    }
    /**
     * 锟斤拷询全锟斤拷
     */
    @ResponseBody
    @RequestMapping("/lists")
    @SystemControllerLog(description="锟斤拷询学锟斤拷锟斤拷息")
    public DataTableResponse<Student> slist(DataTableRequest dataTableRequest){
        dt=studentService.getAllUser(dataTableRequest);
        return dt;
    }
    /**
     * 锟斤拷锟斤拷锟斤拷息
     */
    @ResponseBody
    @RequestMapping("/add")
    public ResponseMessage add(HttpServletRequest request,Student student){
        List<Map<String,Object>> list=ToolUtils.uploadUtil(request,"D:/webWorkspace/apache-tomcat-8.0.24/file");
        System.out.println(list.size()+"ddddd");
        for(Map<String,Object> map:list){
            System.out.println(map.get("fileName"));
        }
        System.out.println(student);
        if(student!=null){
            int res=studentService.add(student);
            if(res<=0){
                responseMessage.setStatus("fail");
            }
            responseMessage.setStatus("success");
        }
        return responseMessage;
    }
    /**
     * 锟斤拷询学校
     */
    @ResponseBody
    @RequestMapping("/school")
    public ResponseMessage getSchool(){
        List<School> list = schoolService.list();
        List<Map<String,Object>> lt=new ArrayList<Map<String,Object>>();
        Map<String,Object> map;
        for(School l:list){
            map=new HashMap<String,Object>();
            map.put("schoolName",l.getSchoolName());
            lt.add(map);
        }
        responseMessage.setData(lt);
        responseMessage.setStatus("fail");
        return responseMessage;
    }
    /**
     * 锟斤拷取一锟斤拷
     */
    @ResponseBody
    @RequestMapping("/get")
    public Student demoGet(@RequestParam Integer id){
        student=studentService.get(id);
        return student;
    }
    /**
     * 删锟斤拷
     */
    @ResponseBody
    @RequestMapping("/delete")
    public ResponseMessage demoDelete(@RequestParam String idString){
        List<Integer> idList=ToolUtils.splitIdString(idString);
        for(Integer id:idList){
            studentService.delete(id);
        }
        responseMessage.setStatus("success");
        return responseMessage;
    }
    /**
     * 锟睫革拷
     */
    @ResponseBody
    @RequestMapping("/update")
    public ResponseMessage demoUpdate(Student student){
        int res=studentService.update(student);
        if(res<=0){
            responseMessage.setStatus("fail");
        }
        responseMessage.setStatus("success");
        return responseMessage;
    }
    /**
     * 锟斤拷锟斤拷锟侥硷拷
     */
    @RequestMapping("/download")
    public ResponseEntity<byte[]> downloadFile(){
        return ToolUtils.downloadUtil("D:/me/apache-tomcat-8.0.24/file/synnex.sql","sss.sql");
    }
}
